package Games.PumpGame.Gui
{
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import Framework.BaseGui;
	import flash.events.KeyboardEvent;
	import flash.display.Bitmap;
	import Framework.KeyEvents.KeyEvent;
	import Games.PumpGame.EventHandler;
	/**
	 * ...
	 * @author Menno
	 */
	public class AvatarVisual extends BaseGui
	{
		private var characterImage:Bitmap;
		private var acceleration:int = 10;
		
		private var _currentCarry:int = 0;
		private var _maxCarry:int = 100;
		
		private var energyBar:Sprite;
		
		public function AvatarVisual() 
		{						
			[Embed(source = '../Images/Character.png')]
			var image:Class;
			characterImage = new image();
			
			energyBar = new Sprite();
			addChild(energyBar);
			
			addEventListeners();
		}		
		
		public function update():void
		{
			drawEnergyBar();
		}
		
		override public function draw():void
		{		
			characterImage.height = 40;
			characterImage.width = 40;
			
			characterImage.x = 0 - characterImage.width / 2;
			characterImage.y = 0 - characterImage.height / 2;	
			
			this.addChild(characterImage);
			
			drawEnergyBar();
			
			this.x = 200;
			this.y = 100;			
			
		}
		
		public function drawEnergyBar():void
		{
			var thickness:int = 2;
			var barWidth:int = 20;
			var borderX:int = 0 + characterImage.width / 2;
			var borderY:int = 0 - characterImage.height / 2
			
			energyBar.graphics.clear();
			energyBar.graphics.lineStyle(thickness, 0xFFFFFF);
			energyBar.graphics.drawRect(borderX , borderY, barWidth, characterImage.height);
			
			trace(currentCarry, maxCarry);
			var percentile:Number = currentCarry / maxCarry;
			var newHeight:int = characterImage.height  * percentile// - 2 * thickness;
			var startY:int = characterImage.height - newHeight// - 2 * thickness;
			
			energyBar.graphics.beginFill(0xFF0000);
			energyBar.graphics.drawRect(borderX, startY - characterImage.height /2 , barWidth, newHeight);
			energyBar.graphics.endFill();
		}
		
		public function addEventListeners():void
		{
			EventHandler.myDispatcher.addEventListener(KeyEvent.ARROW_RIGHT, move);
			EventHandler.myDispatcher.addEventListener(KeyEvent.ARROW_LEFT, move);
			EventHandler.myDispatcher.addEventListener(KeyEvent.ARROW_UP, move);
			EventHandler.myDispatcher.addEventListener(KeyEvent.ARROW_DOWN, move);
		}
		
		private function move(e:KeyEvent):void 
		{
			if (e.type == KeyEvent.ARROW_DOWN)
			{
				this.y += 1*acceleration;
			}
			if (e.type == KeyEvent.ARROW_UP)
			{
				this.y -= 1*acceleration;
			}
			if (e.type == KeyEvent.ARROW_LEFT)
			{
				this.x -= 1*acceleration;
			}
			if (e.type == KeyEvent.ARROW_RIGHT)
			{
				this.x += 1*acceleration;
			}
		}
		
		private function moveRight(e:KeyEvent):void 
		{
			//trace("Event Recieved.");
		}

		//////////////////////////////////////////////////
		//												//
		// 				PROPERTIES						//
		//												//
		//////////////////////////////////////////////////	
		
		public function get currentCarry():int 
		{
			return _currentCarry;
		}
		
		public function set currentCarry(value:int):void 
		{
			_currentCarry = value;
		}
		
		public function get maxCarry():int 
		{
			return _maxCarry;
		}
		
		public function set maxCarry(value:int):void 
		{
			_maxCarry = value;
		}

	}
	
}